package com.alfred.springboot.jdbctemplate.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.ConnectionCallback;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.sql.ResultSet;
import java.sql.Statement;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author: alfred
 * @date: 2021/7/19
 * @description: 测试接口
 */
@RestController
public class TestController {

    @Autowired
    JdbcTemplate jdbcTemplate;

    @GetMapping("/test")
    public Map<String, Object> test(){
//        List<Map<String, Object>> list = jdbcTemplate.queryForList("select * FROM sys_user");
//        return list.get(0);
        // 需要允许多语句查询
        Object obj = jdbcTemplate.execute((ConnectionCallback<Long>) conn -> {
            try (Statement stmt = conn.createStatement()) {
                stmt.execute("UPDATE CDMS_SEQUENCE SET CURRENT_VAL = CURRENT_VAL + INCREMENT_VAL WHERE SEQ_NAME = 'taskschedule_task' RETURNING CURRENT_VAL;");
                try (ResultSet rs = stmt.getResultSet()) {
                    if (rs.next()) {
                        return rs.getLong(1);
                    }
                }
            }
            return null;
        });
        return new HashMap<String, Object>() {{
            put("num", obj);
        }};
    }

}
